﻿cmake_minimum_required(VERSION 3.15)

# 设置一个自定义变量来控制是否使用Clang编译
set(ENABLE_CLANG "OFF" CACHE STRING "Use or Not Use  Clang")
if(ENABLE_CLANG STREQUAL "ON")
# 设置C编译器为clang 
set(CMAKE_C_COMPILER "clang")
# 设置C++编译器为clang++
set(CMAKE_CXX_COMPILER "clang++")

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wno-deprecated-declarations")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wvla-cxx-extension")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wnon-c-typedef-for-linkage")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wenum-constexpr-conversion")

endif ()


project(SmartAudioManagerGW CXX)


# 替换为跨平台检测方式
if(WIN32)
    # 获取 Windows 系统信息
    cmake_host_system_information(RESULT OS_NAME QUERY OS_NAME)
    cmake_host_system_information(RESULT OS_RELEASE QUERY OS_RELEASE)
    cmake_host_system_information(RESULT OS_VERSION QUERY OS_VERSION)
    cmake_host_system_information(RESULT OS_PLATFORM QUERY OS_PLATFORM)
    
    set(OS_INFO "${OS_NAME} ${OS_RELEASE} ${OS_VERSION} (${OS_PLATFORM})")
elseif(UNIX AND NOT APPLE)
    # 获取 Linux 系统信息
    find_program(LSB_RELEASE lsb_release)
    if(LSB_RELEASE)
        execute_process(
            COMMAND ${LSB_RELEASE} -d -s
            OUTPUT_VARIABLE OS_INFO
            OUTPUT_STRIP_TRAILING_WHITESPACE
        )
    else()
        set(OS_INFO "Linux (无法获取详细信息)")
    endif()
elseif(APPLE)
    # 获取 macOS 系统信息
    execute_process(
        COMMAND sw_vers -productName
        OUTPUT_VARIABLE OS_NAME
        OUTPUT_STRIP_TRAILING_WHITESPACE
    )
    execute_process(
        COMMAND sw_vers -productVersion
        OUTPUT_VARIABLE OS_VERSION
        OUTPUT_STRIP_TRAILING_WHITESPACE
    )
    set(OS_INFO "${OS_NAME} ${OS_VERSION}")
else()
    set(OS_INFO "未知操作系统")
endif()

message(STATUS "操作系统信息: ${OS_INFO}")


message(STATUS "------------------------------------------------------------")
message(STATUS "系统发行版本:${DISTRO_ID}")
message(STATUS "项目名:${PROJECT_NAME}")
message(STATUS "系统架构:${CMAKE_SYSTEM_PROCESSOR}")
# 设置版本号变量
set(VERSION_MAJOR 3)
set(VERSION_MINOR 0)
set(VERSION_PATCH 1)
set(VERSION_BUILD 0)
# 生成版本头文件
configure_file(version.h.in ${CMAKE_CURRENT_LIST_DIR}/version.h)
message(STATUS "项目版本: ${VERSION_MAJOR}.${VERSION_MINOR}.${VERSION_PATCH}.${VERSION_BUILD}")

set(CMAKE_BUILD_TYPE Debug)
# set(CMAKE_BUILD_TYPE Release)

# add_compile_options(-Wformat-overflow)
# add_compile_options(-Wno-deprecated-declarations)
# 设置打印编译过程
set(CMAKE_VERBOSE_MAKEFILE ON)
# 设置一个自定义变量来控制是否使用CLion
set(ENABLE_CLION "OFF" CACHE STRING "Enable or disable CLion IDE")

include(CheckIncludeFileCXX)

check_include_file_cxx(any HAS_ANY)
check_include_file_cxx(string_view HAS_STRING_VIEW)
check_include_file_cxx(coroutine HAS_COROUTINE)
if (NOT "${CMAKE_CXX_STANDARD}" STREQUAL "")
    # Do nothing
elseif (HAS_ANY AND HAS_STRING_VIEW AND HAS_COROUTINE)
    set(CMAKE_CXX_STANDARD 20)
elseif (HAS_ANY AND HAS_STRING_VIEW)
    set(CMAKE_CXX_STANDARD 17)
else ()
    set(CMAKE_CXX_STANDARD 14)
endif ()


set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)
# 生成编译数据库
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)


add_executable(${PROJECT_NAME} main.cpp)

# 自定义生成目标的名称
set(EXE_NAME "ebs-${DISTRO_ID}-${VERSION_MAJOR}.${VERSION_MINOR}.${VERSION_PATCH}.${VERSION_BUILD}-${CMAKE_BUILD_TYPE}")
set_target_properties(${PROJECT_NAME} PROPERTIES OUTPUT_NAME ${EXE_NAME})
message(STATUS "可执行文件: ${EXE_NAME}")

message(STATUS "C++标准 ${CMAKE_CXX_STANDARD}")


# 启用 FetchContent 模块
include(FetchContent)

# 启用详细的下载输出
set(FETCHCONTENT_QUIET OFF CACHE BOOL "Disable FetchContent's quiet downloads" FORCE)

# 配置 fmt 库
# GIT_REPOSITORY https://github.com/fmtlib/fmt.git
FetchContent_Declare(
    fmt
    GIT_REPOSITORY https://gitee.com/oss-mirror-github/fmt.git
    GIT_TAG 10.1.1  # 使用兼容的版本
)
FetchContent_MakeAvailable(fmt)

# 配置 spdlog
# GIT_REPOSITORY https://github.com/gabime/spdlog.git
FetchContent_Declare(
    spdlog
    GIT_REPOSITORY https://gitee.com/suncanfly/spdlog.git
    GIT_TAG v1.x  # 指定要使用的版本，v1.x 表示最新的稳定版本
)

# 可选：设置 spdlog 编译选项
set(SPDLOG_BUILD_EXAMPLES OFF CACHE BOOL "" FORCE)
set(SPDLOG_BUILD_TESTS OFF CACHE BOOL "" FORCE)
set(SPDLOG_INSTALL OFF CACHE BOOL "" FORCE)

# 使能对 SPDLOG_FMT_EXTERNAL 的支持
set(SPDLOG_FMT_EXTERNAL ON CACHE BOOL "" FORCE)

# 下载并编译 spdlog
FetchContent_MakeAvailable(spdlog)



aux_source_directory(Log Log_SRC)
aux_source_directory(mDNS mDNS_SRC)
aux_source_directory(Utility/CJson Utility_CJson_SRC)

target_sources(${PROJECT_NAME}
               PRIVATE
               ${Log_SRC}
               ${mDNS_SRC}
               ${Utility_CJson_SRC}
               )



# 链接 spdlog 库
target_link_libraries(${PROJECT_NAME} PRIVATE spdlog::spdlog)

# 如果你需要额外的编译选项，可以在这里添加
# target_compile_options(my_app PRIVATE -Wall -Wextra)
